package com.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.demo.service.KubernetesService;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1NodeList;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.openapi.models.V1ServiceList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/api/k8s")
public class KubernetesController {

    private static final Logger log = LoggerFactory.getLogger(KubernetesController.class);
    private final KubernetesService kubernetesService;

    @Autowired
    public KubernetesController(KubernetesService kubernetesService) {
        this.kubernetesService = kubernetesService;
    }

    @GetMapping("/nodes")
    public V1NodeList getNodes() throws Exception {
        return kubernetesService.listNodes();
    }

    /**
     * <a href="http://localhost:8081/api/k8s/services">...</a>
     * @return
     * @throws Exception
     */
    @GetMapping("/services")
    public V1ServiceList getServices() throws Exception {
        V1ServiceList v1ServiceList = kubernetesService.listServices();
        return v1ServiceList;
    }

    /**
     * <a href="http://localhost:8081/api/k8s/pods">...</a>
     * @return
     * @throws IOException
     * @throws ApiException
     */
    @GetMapping("/pods")
    public V1PodList getPods() throws IOException, ApiException {
        V1PodList v1PodList = kubernetesService.listPodsForAllNamespaces();
        return v1PodList;
    }
}
